package com.y.report.controller;

import com.google.common.collect.Maps;
import com.y.report.entity.basic.YReportApi;
import com.y.report.entity.basic.YReportDs;
import com.y.report.entity.basic.YReportSql;
import com.y.report.entity.common.Q;
import com.y.report.entity.common.R;
import com.y.report.entity.common.U;
import com.y.report.entity.mapper.ApiReportInfo;
import com.y.report.service.ReportService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.tuple.MutableTriple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/y-report")
@Api(tags = "主业务控制器")
public class ReportContoller {

    @Autowired
    private ReportService reportService;

    @GetMapping("/list")
    @ApiOperation("查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiUrl", value = "请求api url", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "apiName", value = "请求api名称", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "apiCode", value = "请求api编码", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "apiAuth", value = "请求api认证信息", dataType = "string", paramType = "query", required = false),
            @ApiImplicitParam(name = "apiMethod", value = "请求api方法", dataType = "string", paramType = "query", required = false)
    })
    protected R list(
            @RequestParam String apiUrl,
            @RequestParam(required = false) String apiName,
            @RequestParam(required = false) String apiCode,
            @RequestParam(required = false) String apiAuth,
            @RequestParam(required = false) String apiMethod
    ) {
        return R.success(reportService.getApiByUriOrName(apiName, apiCode, apiAuth, apiUrl, apiMethod, null));
    }

    @ApiOperation("添加")
    @PostMapping("/append")
    protected R append(@ApiParam(name = "需要新增的实体", required = true) @RequestBody ApiReportInfo append) {
        Assert.notNull(append, "插入参数不能为空");
        int count = reportService.append(append);
        return count == 1 ? R.ok() : R.fail("插入不成功");
    }

    @ApiOperation("修改")
    @PutMapping("/modify")
    protected R modify(@ApiParam(name = "需要修改的实体", required = true) @RequestBody U<ApiReportInfo> modify) {
        Assert.notNull(modify.getQueryObj(), "修改参数不能为空");
        Assert.notNull(modify.getModifyObj(), "修改参数不能为空");
        int count = reportService.modifyOne(modify.getQueryObj(), modify.getModifyObj());
        return count == 1 ? R.ok() : R.fail("修改不成功");
    }


    @PostMapping("/query")
    @ApiOperation("查询列表")
    public R query(@RequestBody Q<ApiReportInfo> queryObj) {
        R<MutableTriple<List<YReportApi>, List<YReportSql>, List<YReportDs>>> tupleResult = R.success(reportService.queryCondition(queryObj.getEqObj(), queryObj.getLikeObj(), queryObj.getPage()));
        MutableTriple<List<YReportApi>, List<YReportSql>, List<YReportDs>> content = tupleResult.getContent();
        Map<String, Object> result = Maps.newHashMap();
        if (content.getLeft() != null)
            result.put(YReportApi.class.getSimpleName(), content.getLeft());
        if (content.getMiddle() != null)
            result.put(YReportSql.class.getSimpleName(), content.getMiddle());
        if (content.getRight() != null)
            result.put(YReportDs.class.getSimpleName(), content.getRight());
        if (queryObj.getPage() != null)
            tupleResult.setExtData(queryObj.getPage());
        return R.success(result.size() == 1 ? result.values().iterator().next() : result);
    }

}
